﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

using LowLevelGraphics.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class ToDo1 : BaseImageFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ToDo1"/> class.
        /// </summary>
        public ToDo1()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ToDo1"/> class.
        /// </summary>
        /// <param name="_todo1">The _todo1.</param>
        internal ToDo1(ToDo1 _todo1)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            List<ColorRect> m_aRect = new List<ColorRect>();
            List<ColorRect> m_aRect2 = new List<ColorRect>();
            Hashtable m_aHashrect = new Hashtable();


            List<KeyValuePair<int, int>> aCoord = new List<KeyValuePair<int, int>>();
            aCoord.Add(new KeyValuePair<int, int>(-1, -1));
            aCoord.Add(new KeyValuePair<int, int>(0, -1));
            aCoord.Add(new KeyValuePair<int, int>(1, -1));

            aCoord.Add(new KeyValuePair<int, int>(-1, 0));
            aCoord.Add(new KeyValuePair<int, int>(1, 0));

            aCoord.Add(new KeyValuePair<int, int>(-1, 1));
            aCoord.Add(new KeyValuePair<int, int>(0, 1));
            aCoord.Add(new KeyValuePair<int, int>(1, 1));

            UnsafeBitmap bitmap = _bitmap;

            int nCount = 0;
            List<Color> aColor = new List<Color>() { bitmap.GetPixel(0, 0) };
            nCount = 1;
            //1. Durchgang


            Point ptStart = new Point(bitmap.Width / 2, bitmap.Height / 2);
            Rectangle temp = Rectangle.Empty;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            Color color = Color.Transparent;
            Color colorFirst = Color.Transparent;
            Color colorNext = Color.Transparent;

            int y = 0;
            int x = 0;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    aColor.Add(color);

                    colorFirst = aColor[nCount - 1];
                    colorNext = aColor[nCount];

                    //Nächste Farbe ok???
                    //nächstes passendes Pixel suchen

                    if (ColorFunctions.IsInColorTolerance(colorFirst, colorNext, 6, 6, 6))
                    {
                        if (temp == Rectangle.Empty)
                        {
                            temp = new Rectangle(x, y, 1, 1);
                        }
                        else
                        {
                            temp = Rectangle.Union(temp, new Rectangle(x, y, 1, 1));
                        }
                    }
                    else
                    {
                        m_aRect2.Add(new ColorRect(Color.Red, temp));
                        temp = Rectangle.Empty;
                    }

                    //keins mehr gefunden? Nächstes Rechteck

                    nCount++;
                }
            }

/*            _bitmap.UnlockImage();
            Graphics g = Graphics.FromImage(_bitmap.InternalBitmap);
            int i = 0;
            for (i = 0; i < m_aRect2.Count; i++)
            {
                Rectangle rect = m_aRect2[i].Rectangle;
                for (y = m_aRect2[i].Rectangle.Bottom; ; y++)
                {
                    for (x = 0; x < m_aRect2[i].Rectangle.Width; x++)
                    {
                        g.DrawRectangle(new Pen(m_aRect2[i].Color), m_aRect2[i].Rectangle);
                    }
                }
            }*/

            UnsafeGraphics g = new UnsafeGraphics(_bitmap);
            int i = 0;
            for (i = 0; i < m_aRect2.Count; i++)
            {
                Rectangle rect = m_aRect2[i].Rectangle;
                for (y = m_aRect2[i].Rectangle.Bottom; ; y++)
                {
                    for (x = 0; x < m_aRect2[i].Rectangle.Width; x++)
                    {
                        g.DrawRectangle(new Pen(m_aRect2[i].Color), m_aRect2[i].Rectangle);
                    }
                }
            }

            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new ToDo1(this);
        }
    }
}
